home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 6 / cmerge.zip / CMERGE.C next >
Text File  |  1987-06-08  |  14KB  |  422 lines

  1. /*                                                                           */
  2. /*      cmerge - merge an assembly language and C file                       */
  3. /*                                                                           */
  4. /*      replace ; Line <number> lines in the assembly output with            */
  5. /*      lines from the original C source as comments                         */
  6. /*                                                                           */
  7.  
  8. #include <stdio.h>
  9.  
  10.  
  11. #define MAXLINE 256                     /* maximum line size                 */
  12. #define MAXFNAME 80                     /* maximum filename size             */
  13.  
  14. #define errno 1
  15.  
  16. char    *lst_ptr;                       /* ptr to assy line (LST) file       */
  17. char    *ext_ptr;                       /* extension pointer                 */
  18. char    aline[MAXLINE];                 /* assembly line                     */
  19. char    cline[MAXLINE];                 /* C source line                     */
  20. char    opt_ar[MAXFNAME];               /* array for option name             */
  21. char    out_ar[MAXFNAME];               /* output filename                   */
  22. char    inp_ar[MAXFNAME];               /* input assembly filename           */
  23. char    pagestr[MAXLINE];
  24. char    mypagestr[MAXLINE];
  25. char    pagefound = 0;
  26. int     output_code;                    /* output file code                  */
  27. int     input_code;                     /* input file code                   */
  28. int     opt_cnt;                        /* input line option count/index     */
  29. unsigned clinenumber=0;                 /* C line counter                    */
  30. unsigned pageno=0;
  31. unsigned linecnt=0;
  32.  
  33. FILE    *cfp,*afp;                      /* C, assembly FILE pointer          */
  34. FILE    *ofp;                           /* output file pointer               */
  35.  
  36. char tolower(char);
  37.  
  38. main(argc,argv)
  39. int     argc;
  40. char    **argv;
  41.  
  42.     {
  43.     unsigned alinenumber;           /* assy line counter                 */
  44.     opt_cnt = 2;                    /* initialize option count/index     */
  45.     output_code = 0;                /* output code = default (stdout)    */
  46.     input_code = 0;                 /* input code=default (.LST file)    */
  47.  
  48.     if(argc < 2 || argc > 4)        /* improper input line count         */
  49.         {                       /* terminate program                 */
  50.                 fprintf(stderr,"cmerge: usage cmerge <name> [-a] [-a=<file>] [-o] [-o=<file>]\n");
  51.         exit(1);
  52.         }
  53.  
  54. /* --------------------- decode options ------------------------------------ */
  55.  
  56.      while((argc-1) >= opt_cnt)             /* if options exist, decode  */
  57.         {
  58.         strcpy(opt_ar, argv[opt_cnt]);  /* copy option into array    */
  59.  
  60.         if(opt_ar[0] != '-')            /* '-' does not precede opt. */
  61.             {
  62.             fprintf(stderr,"cmerge: illegal option syntax;\n");
  63.             fprintf(stderr,"\t[-a] [-a=<file>] [-o] [-o=<file>]\n");
  64.             exit(errno);
  65.             }
  66.  
  67.         if(tolower(opt_ar[1]) == 'a')   /* 'a' option-redirect input */
  68.             {
  69.             if(dec_input() == 0)        /* decode input option       */
  70.             {
  71.             exit(errno);
  72.             }
  73.             }
  74.  
  75.         else if(tolower(opt_ar[1]) == 'o') /* 'o' option-redirect output*/
  76.             {
  77.                       if(dec_output() == 0)       /* decode output option      */
  78.             {
  79.             exit(errno);
  80.             }
  81.             }
  82.  
  83.         else                            /* illegal option, terminate*/
  84.             {
  85.             fprintf(stderr,"cmerge: '-%c' undefined  option",opt_ar[1]);
  86.             fprintf(stderr,"        [-a] [-o]\n");
  87.             exit(errno);
  88.             }
  89.  
  90.         ++opt_cnt;                      /* inc option index          */
  91.         }
  92. /* ------------------------------------------------------------------------- */
  93.  
  94.     if(set_io(argv) == 0)                   /* set redirection (i/o)     */
  95.         {
  96.         exit(errno);
  97.         }
  98.  
  99.     strcpy(cline, argv[1]);                 /* set C input filename      */
  100.     strcat(cline, ".c");
  101.  
  102.     if(output_code != 0)                    /* open output file          */
  103.         {
  104.         if((ofp = fopen(out_ar, "w")) == NULL)
  105.             {
  106.             fprintf(stderr,"cmerge: error opening output file ");
  107.             fprintf(stderr,"%s\n", out_ar);
  108.             exit(errno);
  109.             }
  110.         }
  111.         else                            /* output code = 0           */
  112.             {
  113.             ofp = stdout;               /* set output = stdout       */
  114.             }
  115.  
  116.     if((cfp=fopen(cline,"r")) == NULL)      /* open C source file        */
  117.         {
  118.         fprintf(stderr,"cmerge: can't open %s\n",cline);
  119.         exit(errno);
  120.         }
  121.  
  122.     if((afp=fopen(aline,"r")) == NULL)      /* open assembly sorurce file*/
  123.         {
  124.         fprintf(stderr,"cmerge: can't open %s\n",aline);
  125.         exit(errno);
  126.         }
  127.  
  128. /* --------------------- output -------------------------------------------- */
  129.  
  130.     while(fgets(aline,MAXLINE,afp) != NULL) /* output assy/C source files*/
  131.         {
  132.         lst_ptr = aline;                /* set ptr to  aline         */
  133.         while(isspace(*lst_ptr))        /* skip lead in spaces       */
  134.             {
  135.             lst_ptr++;
  136.             }
  137.  
  138.         if(strncmp(lst_ptr,"; Line ",7) == 0)   /*search for C source*/
  139.             {                               /* insert flagNETWORK/
  140.                         /* insert C source @ flag    */
  141.             *lst_ptr = '\0';        /* insert delimiter          */
  142.             alinenumber=atoi(&lst_ptr[7]);  /*C source line count*/
  143.             while(clinenumber < alinenumber) /* insert C source  */
  144.  
  145.                 {
  146.                 strcpy(cline,";|*** ");
  147.                 if(fgets(cline+6,MAXLINE-2,cfp) != NULL)
  148.                     {
  149.                                  fputs(cline, ofp);  /* insert C src. line*/
  150.                                     checkpage();
  151.                     }
  152.                 ++clinenumber;          /* inc C line #      */
  153.                 }
  154.                         fprintf(ofp,"; Line %d\n",alinenumber);
  155.                         checkpage();
  156.                }
  157.         else
  158.                {
  159.                          if(strncmp(lst_ptr+1,"Microsoft",9) == 0)
  160.                            continue;
  161.                          else
  162.                            if (strstr(aline,"Page"))
  163.                              {
  164.                                pagefound = 1;
  165.                                strcpy(mypagestr,aline);
  166.                          }
  167.                          else
  168.                               {
  169.                                 fputs(aline, ofp);              /* output assy.text  */
  170.                                 checkpage();
  171.                               }
  172.                         }
  173.         }
  174.  
  175.                         /* output remaining C stuff  */
  176.  
  177.     while(fgets(cline,MAXLINE,cfp) != NULL)
  178.            {
  179.         fputs(cline, ofp);                      /* output C src. line*/
  180.                 checkpage();
  181.            }
  182.     }
  183.  
  184. #include <time.h>
  185.  
  186. checkpage()
  187. {
  188.    long secs_now;
  189.    char *str_now;
  190.  
  191.  
  192.    if (pagefound)
  193.      if ((linecnt++ % 60) == 0)
  194.        {
  195.          time(&secs_now);
  196.          str_now = ctime(&secs_now);
  197.          pageno++;
  198.          itoa(pageno,pagestr,10);
  199.          strncpy(mypagestr,"Cmerge V1.0",strlen("Cmerge V1.0"));
  200.          strncpy(mypagestr+strlen("Cmerge V1.0")+5,str_now,strlen(str_now)-1);
  201.          strcpy(strchr(mypagestr,'-')+1,pagestr);
  202.          fputs("\014",ofp);
  203.          fputs(mypagestr,ofp);
  204.          fputs("\n\n",ofp);
  205.          ++linecnt;
  206.        }
  207. }
  208.  
  209. /* ------------------------------------------------------------------------- */
  210. /* -    dec_input(); decode input options                                  - */
  211. /* ------------------------------------------------------------------------- */
  212.  
  213. int dec_input()
  214.  
  215. {
  216.     if(input_code != 0)                     /* input already redirected  */
  217.         {
  218.         fprintf(stderr,"cmerge: conflicting input redirection\n");
  219.         return(0);                          /* return w/ error           */
  220.         }
  221.  
  222.     if(opt_ar[2] == '\0')                   /* input from name.ASM       */
  223.         {
  224.         input_code = 1;                     /* set input code            */
  225.         }
  226.  
  227.     else if(opt_ar[2] == '=')               /* input from redifined name */
  228.         {
  229.         strcpy(inp_ar, &opt_ar[3]);         /* copy f-name into input ar */
  230.         if((strchr(inp_ar, '.')) == 0)       /* is ext. specified  */
  231.         {
  232.         input_code = 2;                 /* f-name w/o extension      */
  233.         }
  234.         else
  235.         {
  236.         input_code = 3;                 /* f-name w/ extension       */
  237.         }
  238.         }
  239.  
  240.     else                                    /* unrecognizable option     */
  241.         {
  242.         fprintf(stderr,"cmerge: syntax error; input option\n");
  243.         return (0);                         /* return w/ error           */
  244.         }
  245.  
  246.     return(1);                              /* return w/o error          */
  247. }
  248.  
  249. /* ------------------------------------------------------------------------- */
  250. /* -    dec_output(); decode output options                                - */
  251. /* ------------------------------------------------------------------------- */
  252.  
  253. int dec_output()
  254.  
  255. {
  256.     if (output_code != 0)                   /* output already redirected */
  257.         {
  258.         fprintf(stderr,"cmerge: conflicting output redirection\n");
  259.         return (0);                         /* return w/ error           */
  260.         }
  261.  
  262.     if(opt_ar[2] == '\0')                   /*output to f-name w/CLS ext */
  263.         {
  264.         output_code = 1;                    /* set output code           */
  265.         }
  266.     else if(opt_ar[2] == '=')               /* set output to new filename*/
  267.         {
  268.         strcpy(out_ar, &opt_ar[3]);         /* copy filename into out_ar */
  269.         if(strchr(out_ar, '.') == 0)         /* search for extension      */
  270.         {
  271.         output_code = 2;                /* no extension specified    */
  272.         }
  273.          else
  274.         {
  275.         output_code = 3;                /*extension specified        */
  276.         }
  277.         }
  278.  
  279.     else                                    /* unrecognizable option     */
  280.         {
  281.         fprintf(stderr,"cmerge: syntax error; output option\n");
  282.         return (0);                         /* return w/o error          */
  283.         }
  284.  
  285.     return (1);                             /* return w/o error          */
  286. }
  287.  
  288. /* ------------------------------------------------------------------------- */
  289. /* -    set_io (); set the assembly file input and file output             - */
  290. /* ------------------------------------------------------------------------- */
  291.  
  292. int set_io (argv)
  293.  
  294. char    **argv;
  295. {
  296.  
  297.       /***** set assembly input *****/
  298.  
  299.     if(input_code == 0)                     /* default input, "name.LST" */
  300.         {
  301.         strcpy(aline, argv[1]);             /* create assembly filename  */
  302.         strcat(aline, ".LST");
  303.         }
  304.  
  305.     else if(input_code == 1)                /* input; "name.ASM"         */
  306.         {
  307.         strcpy(aline, argv[1]);             /* create assembly filename  */
  308.         strcat(aline, ".asm");
  309.         }
  310.  
  311.     else if(input_code == 2)                /* input; specified name. */
  312.         {
  313.         strcpy(aline, inp_ar);              /* create assembly filename  */
  314.         strcat(aline, ".LST");
  315.         }
  316.  
  317.     else if(input_code == 3)                /* input; f-name.ext         */
  318.         {
  319.         strcpy(aline, inp_ar);              /* create assembly filename  */
  320.         }
  321.  
  322.     else                                    /* error                     */
  323.         {
  324.         fprintf(stderr,"cmerge: error redirecting input;\n");
  325.         fprintf(stderr,"        undefined code\n");
  326.         return (0);                         /* return w/ error           */
  327.         }
  328.  
  329.     /***** set file output *****/
  330.  
  331.     if(output_code == 0)                    /* default output = stdout   */
  332.         {
  333.         ofp = stdout;                       /* set std output            */
  334.         }
  335.  
  336.     else if(output_code == 1)               /* output; assy. "name"      */
  337.         {
  338.         strcpy(out_ar, argv[1]);            /* create output filename    */
  339.         if((ext_ptr = (strchr(aline, '.'))) == 0)    /* find ext          */
  340.         {
  341.         fprintf(stderr,"cmerge: error redirecting output;\n");
  342.         fprintf(stderr,"        file extension not found\n");
  343.         return (0);                             /* return w/ error   */
  344.         }
  345.  
  346.         else                                /* decode ext                */
  347.         {
  348.         if(strncmp(ext_ptr, ".ASM", 4) == 0)
  349.             {
  350.             strcat(out_ar, ".asl");     /* set ext. = asl            */
  351.             }
  352.  
  353.         else
  354.             {
  355.             strcat(out_ar, ".lsl");     /* set ext. = lsl            */
  356.             }
  357.         }
  358.         }
  359.  
  360.     else if(output_code == 2)               /* output; f-name.x          */
  361.         {                                   /* x = assy. extension       */
  362.         if((ext_ptr =(strchr(aline, '.'))) == 0)  /* search for assy. ext */
  363.         {
  364.         fprintf(stderr,"cmerge: error redirecting output;\n");
  365.         fprintf(stderr,"        file extension not found\n");
  366.         return (0);                             /* return w/ error   */
  367.         }
  368.  
  369.         else
  370.         {
  371.         if(strncmp(ext_ptr,".asm", 4) == 0)     /* if assy = .ASM    */
  372.             {
  373.             strcat(out_ar, ".asl");     /* output will be .asl ext.  */
  374.             }
  375.         else
  376.             {
  377.             strcat(out_ar, ".lsl");     /* output will be .lsl ext.  */
  378.             }
  379.         }
  380.         }
  381.  
  382.     else if((output_code != 0) && (output_code != 3)) /* not default =0  */
  383.         {                                             /* not specif. =3  */
  384.         fprintf(stderr,"cmerge: error redirecting output;\n");
  385.         fprintf(stderr,"        undefined code\n");
  386.         return (0);                                   /* return w/ error */
  387.         }
  388.  
  389.     return (1);                             /* return w/o error          */
  390. }
  391.  
  392. /*                                    */
  393. /*    tolower - convert the given character to lower case        */
  394. /*                                    */
  395.  
  396. char tolower(ch)
  397.     char    ch;
  398.     {
  399.     return isupper(ch) ? ch+('a'-'A') : ch;
  400.     }
  401.  
  402. /*                                    */
  403. /*    isupper - return TRUE if the given character if uppercase    */
  404. /*                                    */
  405.  
  406. isupper(ch)
  407.     char    ch;
  408.     {
  409.     return ch >= 'A' && ch <= 'Z';
  410.     }
  411.  
  412. /*                                    */
  413. /*    isspace - return TRUE if the given character is a whitespace    */
  414. /*                                    */
  415.  
  416. isspace(ch)
  417.     char    ch;
  418.     {
  419.     return ch == ' ' || ch == '\n' || ch == '\t';
  420.     }
  421.  
  422.